// pages/login/index.js
import {
  request
} from '../../request/index.js'
Page({
  /**
   * 页面的初始数据
   */
  member: function () {
    var app = getApp()
    app.globalData.root = this.data.root
    app.globalData.position = 1
    app.globalData.userId = '会员' + app.globalData.userInfo.nickName
    app.globalData.avatar = app.globalData.userInfo.avatarUrl
    if (this.data.userId === '雨') {
      request({
        url: this.data.root + 'api/getUser',
        method: 'GET',
        dataType: 'json',
        data: {
          id: '会员' + app.globalData.userInfo.nickName
        }
      }).then((res) => {
        if (res.data.length === 0) {
          request({
            url: this.data.root + 'api/addUser',
            method: 'POST',
            dataType: 'json',
            data: {
              userName: '会员' + app.globalData.userInfo.nickName + '',
              userPwd: app.globalData.userInfo.nickName,
              userId: '会员' + app.globalData.userInfo.nickName,
              identity: 1,
              avatar: app.globalData.userInfo.avatarUrl
            }
          }).then((res) => {
            wx.switchTab({
              url: '/pages/index/index'
            })
          })
        } else {
          wx.switchTab({
            url: '/pages/index/index'
          })
        }
      })
    } else {
      request({
        url: this.data.root + 'api/addUser',
        method: 'POST',
        dataType: 'json',
        data: {
          userName: app.globalData.userInfo.nickName,
          userPwd: app.globalData.userInfo.nickName,
          userId: app.globalData.userInfo.nickName,
          identity: 1,
          avatar: app.globalData.userInfo.avatarUrl
        }
      }).then((res) => {

        wx.switchTab({
          url: '/pages/index/index'
        })
      })
    }

  },
  coach: function () {
    var app = getApp()
    app.globalData.root = this.data.root
    app.globalData.position = 2
    app.globalData.userId = '教练' + app.globalData.userInfo.nickName
    app.globalData.avatar = app.globalData.userInfo.avatarUrl

    if (this.data.userId === '雨') {
      request({
        url: this.data.root + 'api/getUser',
        method: 'GET',
        dataType: 'json',
        data: {
          id: '教练' + app.globalData.userInfo.nickName
        }
      }).then((res) => {
        if (res.data.length === 0) {
          request({
            url: this.data.root + 'api/addUser',
            method: 'POST',
            dataType: 'json',
            data: {
              userName: '教练' + app.globalData.userInfo.nickName,
              userPwd: app.globalData.userInfo.nickName,
              userId: '教练' + app.globalData.userInfo.nickName,
              identity: 2,
              avatar: app.globalData.userInfo.avatarUrl
            }
          }).then((res) => {
            request({
              url: this.data.root + 'api/addCoach',
              method: 'POST',
              dataType: 'json',
              data: {
                userId: '教练' + app.globalData.userInfo.nickName,
                name: '教练' + app.globalData.userInfo.nickName,
                avatar: app.globalData.userInfo.avatarUrl
              }
            }).then((res) => {
              wx.switchTab({
                url: '/pages/index/index'
              })
            })
          })
        } else {
          wx.switchTab({
            url: '/pages/index/index'
          })
        }
      })

    } else {
      request({
        url: this.data.root + 'api/addUser',
        method: 'POST',
        dataType: 'json',
        data: {
          userName: app.globalData.userInfo.nickName,
          userPwd: app.globalData.userInfo.nickName,
          userId: app.globalData.userInfo.nickName,
          identity: 2,
          avatar: app.globalData.userInfo.avatarUrl
        }
      }).then((res) => {
        request({
          url: this.data.root + 'api/addCoach',
          method: 'POST',
          dataType: 'json',
          data: {
            userId: app.globalData.userInfo.nickName,
            name: app.globalData.userInfo.nickName,
            avatar: app.globalData.userInfo.avatarUrl
          }
        }).then((res) => {
          wx.switchTab({
            url: '/pages/index/index'
          })
        })
      })
    }

  },
  admin: function () {
    var app = getApp()
    app.globalData.root = this.data.root
    app.globalData.position = 3
    app.globalData.userId = '管理员' + app.globalData.userInfo.nickName
    app.globalData.avatar = app.globalData.userInfo.avatarUrl
    if (this.data.userId === '雨') {
      request({
        url: this.data.root + 'api/getUser',
        method: 'GET',
        dataType: 'json',
        data: {
          id: '管理员' + app.globalData.userInfo.nickName
        }
      }).then((res) => {
        console.log(res)
        if (res.data.length === 0) {
          request({
            url: this.data.root + 'api/addUser',
            method: 'POST',
            dataType: 'json',
            data: {
              userName: '管理员' + app.globalData.userInfo.nickName + '',
              userPwd: app.globalData.userInfo.nickName,
              userId: '管理员' + app.globalData.userInfo.nickName,
              identity: 1,
              avatar: app.globalData.userInfo.avatarUrl
            }
          }).then((res) => {
            wx.navigateTo({
              url: '/pages/admin/index'
            })
          })
        } else {
          wx.navigateTo({
            url: '/pages/admin/index'
          })

        }
      })
    }
  },
  data: {
    root: 'http://129.28.168.85:2334/',
    isShow: false,
    userId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var app = getApp()
    var res = app.globalData
    if (res.userInfo.nickName === '雨') {
      this.setData({
        isShow: true,
        userId: res.userInfo.nickName
      })
    } else {
      request({
        url: this.data.root + 'api/getUser',
        method: 'GET',
        dataType: 'json',
        data: {
          id: res.userInfo.nickName
        }
      }).then((res) => {
        if (res.data.length === 0) {
          // 并不存在用户  跳转到注册页面
          this.setData({
            isShow: true
          })
        } else {
          app.globalData.root = this.data.root
          app.globalData.position = res.data[0].identity
          app.globalData.userId = app.globalData.userInfo.nickName
          app.globalData.avatar = app.globalData.userInfo.avatarUrl
          wx.switchTab({
            url: '/pages/index/index'
          })
        }
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})